package com.instalinks.android.apps.service;

import java.util.Enumeration;
import java.util.Hashtable;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.instalinks.android.lib.enums.TimeOuts;

public class IntentBroadcast {

	private Context mContext;

	private Hashtable<String, HandlerInfo> htActionHandler;

	public IntentBroadcast(Context context) {
		this.mContext = context;

		htActionHandler = new Hashtable<String, HandlerInfo>();
	}

	public Handler addHandler(String intentAction, TimeOuts recurringTimeOut,
			String mode, String... fields) {

		if (!htActionHandler.contains(intentAction)) {
			createHandler(intentAction, recurringTimeOut, mode, fields);
		}

		return htActionHandler.get(intentAction).handler;
	}

	public void removeHandler(String intentAction) {

		if (htActionHandler.containsKey(intentAction)) {
			HandlerInfo hInfo = htActionHandler.get(intentAction);

			try {
				hInfo.handler.removeCallbacks(hInfo.runnable);
			} catch (Exception e) {
			}

			htActionHandler.remove(intentAction);
		}
	}

	public void removeAll() {
		Enumeration<String> keys = htActionHandler.keys();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			HandlerInfo hInfo = htActionHandler.get(key);

			try {
				hInfo.handler.removeCallbacks(hInfo.runnable);
			} catch (Exception e) {
			}
		}

		htActionHandler.clear();
	}

	private void createHandler(String intentAction, TimeOuts recurringTimeOut,
			String mode, String... fields) {

		if (!htActionHandler.contains(intentAction)) {

			Handler h = new Handler();

			HandlerInfo hInfo = new HandlerInfo();
			hInfo.handler = h;
			hInfo.intentAction = intentAction;
			hInfo.recurringTimeOut = recurringTimeOut;

			hInfo.mode = mode;
			hInfo.field1 = (fields.length > 0) ? fields[0] : "";
			hInfo.field2 = (fields.length > 1) ? fields[1] : "";
			hInfo.field3 = (fields.length > 2) ? fields[2] : "";
			hInfo.field4 = (fields.length > 3) ? fields[3] : "";

			Runnable r = getRunnableTask(hInfo);
			h.postDelayed(r, TimeOuts.OneSec.getValue());

			hInfo.runnable = r;

			htActionHandler.put(intentAction, hInfo);
		}
	}

	private Runnable getRunnableTask(final HandlerInfo hInfo) {
		Runnable r = new Runnable() {
			@Override
			public void run() {
				sendActionBroadcast(hInfo);
				hInfo.handler.postDelayed(this,
						hInfo.recurringTimeOut.getValue());
			}
		};

		return r;
	}

	private void sendActionBroadcast(HandlerInfo hInfo) {
		Intent i = new Intent();

		i.setAction(hInfo.intentAction);
		Bundle bundle = new Bundle();
		bundle.putString("mode", hInfo.mode);
		bundle.putString("appid", mContext.getPackageName());
		bundle.putString("field1", hInfo.field1);
		bundle.putString("field2", hInfo.field2);
		bundle.putString("field3", hInfo.field3);
		bundle.putString("field4", hInfo.field4);

		i.putExtras(bundle);

		this.mContext.sendBroadcast(i);
	}

	private class HandlerInfo {

		public String intentAction;
		public Handler handler;
		public TimeOuts recurringTimeOut;
		public Runnable runnable;

		public String mode = "time_call";
		public String field1 = "";
		public String field2 = "";
		public String field3 = "";
		public String field4 = "";
	}

}
